package org.example.back.controller.plan;

import org.example.back.entity.plan.BodybuildingPlan;
import org.example.back.service.plan.BodybuildingPlanService;
import org.example.back.service.advice.BodybuildingPlanAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.Map;

@RestController
@RequestMapping("/api/plan/bodybuilding")
public class BodybuildingPlanController {

    @Autowired
    private BodybuildingPlanService service;

    @Autowired
    private BodybuildingPlanAdviceService adviceService;

    @PostMapping("/add")
    public Map<String, Object> addPlan(@RequestBody BodybuildingPlan plan, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        plan.setUserId(userId);
        boolean success = service.addPlan(plan);
        return Map.of("success", success, "message", success ? "添加成功" : "添加失败");
    }

    @GetMapping("/get")
    public Map<String, Object> getPlan(@RequestParam String startDate, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        BodybuildingPlan plan = service.getPlanByUserIdAndStartDate(userId, LocalDate.parse(startDate));
        return Map.of(
            "success", plan != null,
            "message", plan != null ? "查询成功" : "无计划记录",
            "data", plan
        );
    }

    @GetMapping("/advice")
    public Map<String, Object> getAdvice(@RequestParam String startDate, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        String advice = adviceService.getAdvice(userId, LocalDate.parse(startDate));
        return Map.of("success", true, "message", "生成计划成功", "advice", advice);
    }
}
